﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;

namespace xab
{


    /// <summary>
    /// 展示某个Ab包下的所有资源, 一个包可以由多条规则产生(比如: 不同路径的多条同名ExplicitRule)
    /// </summary>
    [Serializable]
    public class BundleInfo
    {

        public BundleName bundleName;

        public CompressType compressType = CompressType.LZMA;

        [HideInInspector]
        public List<AssetInfo> assetInfoList = new List<AssetInfo>();

        private List<AssetInfo> _assetInfoHasBundle;

        public List<string> assetHasBundle = new List<string>();

        public List<string> assetNoBundle = new List<string>();

        public BundleInfo(string name)
        {
            this.bundleName = new BundleName(name);
        }

        /// <summary>
        /// Bundle直接包含的资源
        /// </summary>
        public void AddDirectIncludeAsset(AssetInfo assetInfo)
        {
            if (!assetInfoList.Contains(assetInfo))
            {
                assetInfoList.Add(assetInfo);
                assetInfo.SetOwnBundle(bundleName);
            }
        }

        /// <summary>
        /// Bundle间接依赖的资源
        /// </summary>
        public void AddDepAsset(AssetInfo assetInfo)
        {
            if (!assetInfoList.Contains(assetInfo))
            {
                assetInfoList.Add(assetInfo);
                assetInfo.AddUsedByBundle(bundleName);
            }
        }

        public void Refresh()
        {
            assetHasBundle.Clear();
            assetNoBundle.Clear();

            for (var i = 0; i < assetInfoList.Count; ++i)
            {
                var assetInfo = assetInfoList[i];
                if (null != assetInfo.ownBundle)
                {
                    assetHasBundle.Add(assetInfo.assetPath);
                }
                else
                {
                    assetNoBundle.Add(assetInfo.assetPath);
                }
            }

        }

        public List<AssetInfo> GetAssetInfoHasBundle()
        {
            if (null == _assetInfoHasBundle)
                _assetInfoHasBundle = new List<AssetInfo>();
            else
                _assetInfoHasBundle.Clear();

            for (var i = 0; i < assetInfoList.Count; ++i)
            {
                var assetInfo = assetInfoList[i];
                if (null != assetInfo.ownBundle)
                {
                    _assetInfoHasBundle.Add(assetInfo);
                }
            }

            return _assetInfoHasBundle;
        }

    }


}
